/**
 * Created by steven on 2016/3/28.
 */
$.extend({
    "_options": {
        _detail: function (url, cellvalue) {
            return "<a class=\"btn-link\" href='#' onclick=\"javascript:location.href='" + url + "'\"><span style=\"color:#00acec\">" + cellvalue + "</span></a>";
        },
        _edit: function (url, gridId, idCol) {
            var rowIds = $._options._getSelectedRowIds(gridId);
            var len = rowIds.length;
            if (len < 1) {
                return;
            }

            if (len > 1) {
                ShowMessages("对不起,您只能选中一条记录", "系统提示", "msgwaring", null, 0);
                return;
            }

            location.href = url + "?" + idCol + "=" + $("#" + gridId).jqGrid("getRowData", rowIds[0])[idCol];
        },
        _add: function (url) {
            location.href = url;
        },
        _delete: function (url, gridId, idCol, callback) {
            var rowIds = $._options._getSelectedRowIds(gridId);
            var len = rowIds.length;
            if (len > 0) {
                var _data = [];
                for (var i = 0; i < len; i++) {
                    _data.push($("#" + gridId).jqGrid("getRowData", rowIds[i])[idCol]);
                }

                var _dataStr = idCol + "=" + _data.join("&" + idCol + "=").toString();

                layer.confirm("请确认是否删除选中数据？", {btn: ["确认", "取消"], icon: 3}, function () {
                    $.ajax({
                        url: url,
                        type: "post",
                        data: _dataStr,
                        dataType: "json",
                        success: function (result) {
                            if (typeof callback === "function") {
                                callback();
                            }

                            showOKMessage("删除成功！", "提示信息", null, null);
                        },
                        error: function (error) {
                            showErrorMessage("操作失败，请及时联系管理员，错误信息：" + error, "错误提示", null, null);
                        }
                    })
                }, function () {
                });

            }
        },
        _save: function (url, contanier, callback) {
            $.ajax({
                url: url,
                type: "post",
                data: $(contanier).serialize(),
                dataType: "json",
                success: function (result) {
                    showOKMessage("保存成功！", "提示信息", null, null);
                    if (typeof callback === "function") {
                        callback();
                    }
                },
                error: function (error) {
                    showErrorMessage("操作失败，请及时联系管理员", "错误提示", null, null);
                }
            })
        },
        _query: function (gridId, url) {
            $("#" + gridId).jqGrid('setGridParam', {
                url: url,
                postData: JSON.parse(customFormToData("SearchTable")),
                page: 1
            }).trigger("reloadGrid");
        },
        _refresh: function () {
            location.reload();
        },
        _reload: function (gridId, url, data) {
            var timestamp = new Date().getTime();
            $("#" + gridId).jqGrid('setGridParam', {
                url: url + "?timestamp=" + timestamp,
                postData: data,
                page: 1
            }).trigger("reloadGrid");
        },
        _dictName: function (dictId, dictJson) {
            return dictJson[dictId] || "";
        },
        _getSelectedRowIds: function (gridId) {
            var selectedId = $("#" + gridId).jqGrid("getGridParam", "selrow");
            if (!selectedId) {
                ShowMessages("对不起,您没有选中任何记录", "系统提示", "msgwaring", null, 0);
                return [];
            }

            return $("#" + gridId).jqGrid("getGridParam", "selarrrow");
        },
        _status: function (key, value, settings) {
            return $("<span/>").css(settings[key]._css).addClass(settings[key]._class).text(value).prop("outerHTML");
        }
    }
});


